import pandas as pd
import random
from collections import Counter
from collections import defaultdict


def get_custom_types_list():
    custom_types = []
    with open("custom_types.csv", 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for line in lines:
            new_line = line.split(",")
            new_line = [i.replace("\n", "") for i in new_line]
            custom_types.append("定制类|||||" + new_line[0] + "|||||" + new_line[1] + "_" + new_line[2])
    return custom_types


def tokenize_nmt():
    # CSV文件的路径
    csv_file = 'alarms.csv'  # 替换为你的CSV文件的实际路径和名称
    # 使用pandas的read_csv函数读取CSV文件
    df = pd.read_csv(csv_file, header=None, sep='\t', skiprows=1, encoding='utf-8')

    # 显示数据的前几行来验证读取是否正确
    # print(df.head())

    source, target = [], []
    for index, row in df.iterrows():
        # source_info = [row[0], row[1], row[2], row[4]] + row[3].split(" ")
        source_info = [row[0], row[1], row[2], row[4], row[3]]
        type_info = row[5].split("|||||")
        source.append(source_info)
        target.append(type_info)

    target = ["|||||".join(i) for i in target]
    target_type_list = defaultdict(list)
    for i in range(len(target)):
        target_type_list[target[i]].append(source[i])

    # 使用Counter进行计数
    counts = Counter(target)
    # print(counts)
    # 将Counter对象转换为列表的项（key-value对），然后排序
    sorted_counts = sorted(counts.items(), key=lambda item: item[1], reverse=True)
    # print(sorted_counts)
    # 打印排序后的项（key-value对）

    oldkey2newkey = {}
    custom_types = get_custom_types_list()
    for key, value in sorted_counts:
        find = False
        if key.startswith("定制类"):
            for item in custom_types:
                if key.startswith(item):
                    # print(value,key)
                    find = True
                    oldkey2newkey[key] = key
                    break
            if not find:
                new_key = key.split("|||||")[0] + "|||||" + key.split("|||||")[1] + "|||||" + "Default" + "_" + \
                          key.split("|||||")[3] + "|||||" + key.split("|||||")[3]
                oldkey2newkey[key] = new_key
        else:
            oldkey2newkey[key] = key

    new_target = []
    for item in target:
        if item in oldkey2newkey:
            new_target.append(oldkey2newkey[item])
        else:
            new_target.append(item)

    ########################################################

    target = new_target

    counts = Counter(target)
    sorted_counts = sorted(counts.items(), key=lambda item: item[1], reverse=True)
    id = 0
    types = 0

    alarms_default = defaultdict(list)

    for item, _ in sorted_counts:
        types += 1
        for idx in range(len(source)):
            if target[idx] == item:
                alarms_default[target[idx]].append(source[idx])
                # f.write(str(source[idx]) + "\t" + target[idx] + "\t" + str(counts[item]) + "\n")
                id += 1

    random_sample_size = 200

    new_source_list = []
    new_target_list = []
    for key in alarms_default:
        random_elements = random.choices(alarms_default[key], k=random_sample_size)
        new_target_list.extend([key.split("|||||") for _ in range(random_sample_size)])
        new_source_list.extend(random_elements)

    return new_source_list, new_target_list

# source, target = tokenize_nmt()
# print(size)
# print(size/len(target))
